﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Model;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.SessionState;

namespace BLL.videoLib
{
    public class VideoManager
    {
        public static bool DeleteVideoById(int id)
        {
            IDAL.IVideoDAL videoDAL = DALFactory.VideoDALFactory.CreatInstance("SQLServerVideoDAL");
            return videoDAL.DeleteVideoById(id);
        }

        public static Video GetVideoById(int id)
        {
            IDAL.IVideoDAL videoDAL = DALFactory.VideoDALFactory.CreatInstance("SQLServerVideoDAL");
            return videoDAL.GetVideoById(id);
        }

        public static String GetDetailById_Android(int id)
        {
            IDAL.IVideoDAL videoDAL = DALFactory.VideoDALFactory.CreatInstance("SQLServerVideoDAL");
            Video video = videoDAL.GetVideoById(id);
            return video.VideoName + "|" + video.VideoTypeName + "|" + video.VideoSummary + "|" + video.VideoDirector + "|" + video.VideoActors;
        }

        public static bool AddVideo(Video video)
        {
            IDAL.IVideoDAL videoDAL = DALFactory.VideoDALFactory.CreatInstance("SQLServerVideoDAL");
            return videoDAL.AddVideo(video);
        }

        public static bool UpdateVideo(Video video)
        {
            IDAL.IVideoDAL videoDAL = DALFactory.VideoDALFactory.CreatInstance("SQLServerVideoDAL");
            return videoDAL.UpdateVideo(video);
        }

        public static String GetVideoURLById(int id)
        {
            IDAL.IVideoDAL videoDAL = DALFactory.VideoDALFactory.CreatInstance("SQLServerVideoDAL");
            Video video = videoDAL.GetVideoById(id);
            return "../movies/" + video.VideoURL;
        }

        public static void FillTreeView(TreeView tree)
        {
            IDAL.IVideoDAL videoDAL = DALFactory.VideoDALFactory.CreatInstance("SQLServerVideoDAL");
            Dictionary<String, int> dic = videoDAL.GetAllTypeInfo();

            foreach (KeyValuePair<String, int> ele in dic)
            {
                //此类影片目录结点
                TreeNode categoryNode = new TreeNode(ele.Key);

                // 此类影片结点
                List<Video> list = videoDAL.GetVideosByTypeId(ele.Value);
                TreeNode videoNode = null;
                foreach(Video v in list)
                {
                    videoNode = new TreeNode(v.VideoName);
                    videoNode.NavigateUrl = "./videoLib.aspx?videoId=" + v.VideoId;
                    categoryNode.ChildNodes.Add(videoNode);
                }
                tree.Nodes.Add(categoryNode);
            }

            tree.CollapseAll();
        }

        public static int FillSearchTable(Table table, string keyType, string keyWord, int pageNo)
        {
            IDAL.IVideoDAL videoDAL = DALFactory.VideoDALFactory.CreatInstance("SQLServerVideoDAL");
            List<Video> videoList = videoDAL.SearchVideos(keyType, keyWord);
            int beginItemNo = (pageNo - 1) * 5;
            int endItemNo = ((pageNo - 1) * 5 + 5) < (videoList.Count) ? ((pageNo - 1) * 5 + 5) : (videoList.Count);
            for (int i = beginItemNo ; i < endItemNo; ++i)
            {
                Video video = videoList[i];

                TableRow[] tr = new TableRow[6];
                TableCell[] tc = new TableCell[6];

                for (int j = 0; j != 6; ++j)
                {
                    tr[j] = new TableRow();
                }

                for (int j = 0; j != 6; ++j)
                {
                    tc[j] = new TableCell();
                }

                tc[0].RowSpan = 5;
                tc[0].Text = "<img src='" + video.VideoImage + "' width='97' height='80' border='0' />";
                tr[0].Cells.Add(tc[0]);
                table.Rows.Add(tr[0]);

                tc[1].Text = "名称：<a href='./movieDetailedInfo.aspx?videoId=" + video.VideoId + "'>" + video.VideoName + "</a>";
                tr[1].Cells.Add(tc[1]);
                table.Rows.Add(tr[1]);

                tc[2].Text = "类型：" + video.VideoType;
                tr[2].Cells.Add(tc[2]);
                table.Rows.Add(tr[2]);

                tc[3].Text = "热度指数：" + video.VideohotScore;
                tr[3].Cells.Add(tc[3]);
                table.Rows.Add(tr[3]);

                tc[4].Width = 126;
                tc[4].Text = "&nbsp;";
                tr[4].Cells.Add(tc[4]);
                table.Rows.Add(tr[4]);

                tc[5].Width = 126;
                tc[5].Text = "&nbsp;";
                tr[5].Cells.Add(tc[5]);
                table.Rows.Add(tr[5]);
            }

            TableRow pageNoTr = new TableRow();
            TableCell pageNoTc = new TableCell();
            int TotalPageNum = (videoList.Count/5 + 1);
            String pageNoStr = "第" + pageNo + " / " + TotalPageNum +" 页";
            for (int j = 0; j < TotalPageNum; ++j)
            {
                if (j + 1 != pageNo)
                {
                    pageNoStr += "<a href='./searchResult.aspx?keyType=" + keyType + "&keyWord=" + keyWord + "&pageNo=" + (j+1) + "'>[" + (j+1) + "]</a>&nbsp;";
                }
                else
                {
                    pageNoStr += " " + (j + 1) + " ";
                }
            }

            pageNoTc.HorizontalAlign = HorizontalAlign.Center;
            pageNoTc.Text = pageNoStr;
            pageNoTr.Cells.Add(pageNoTc);
            table.Rows.Add(pageNoTr);

            return videoList.Count;
            
        }

        public static void FillDetailsTable(Table table, int videoId)
        {
            IDAL.IVideoDAL videoDAL = DALFactory.VideoDALFactory.CreatInstance("SQLServerVideoDAL");
            Video video = videoDAL.GetVideoById(videoId);

            table.Width = 568;
            table.BorderStyle = BorderStyle.None;
            table.HorizontalAlign = HorizontalAlign.Left;
            table.CellSpacing = 10;

            TableRow[] tr = new TableRow[9];
            TableCell tc = null;

            for (int i = 0; i < 9; ++i)
            {
                tr[i] = new TableRow();
            }

            tc = new TableCell();
            tc.Width = 162;
            tc.HorizontalAlign = HorizontalAlign.Right;
            tc.Text = "片名：";
            tr[0].Cells.Add(tc);

            tc = new TableCell();
            tc.Width = 199;
            tc.HorizontalAlign = HorizontalAlign.Left;
            tc.Text = video.VideoName;
            tr[0].Cells.Add(tc);

            tc = new TableCell();
            tc.Width = 153;
            tc.RowSpan = 7;
            tc.HorizontalAlign = HorizontalAlign.Center;
            tc.VerticalAlign = VerticalAlign.Middle;
            tc.Text = "<img src='" + video.VideoImage + "' width='155' height='191' align='middle' />";
            tr[0].Cells.Add(tc);

            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Right;
            tc.Text = "导演：";
            tr[1].Cells.Add(tc);

            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Left;
            tc.Text = video.VideoDirector;
            tr[1].Cells.Add(tc);

            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Right;
            tc.Text = "演员：";
            tr[2].Cells.Add(tc);

            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Left;
            tc.Text = video.VideoActors;
            tr[2].Cells.Add(tc);

            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Right;
            tc.Text = "类型：";
            tr[3].Cells.Add(tc);

            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Left;
            tc.Text = video.VideoTypeName.ToString();
            tr[3].Cells.Add(tc);

            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Right;
            tc.Text = "热度：";
            tr[4].Cells.Add(tc);

            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Left;
            tc.Text = video.VideohotScore.ToString();
            tr[4].Cells.Add(tc);

            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Right;
            tc.Text = "语言：";
            tr[5].Cells.Add(tc);

            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Left;
            tc.Text = video.VideoLanguage;
            tr[5].Cells.Add(tc);

            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Right;
            tc.Text = "产地：";
            tr[6].Cells.Add(tc);

            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Left;
            tc.Text = video.VideoNation;
            tr[6].Cells.Add(tc);

            tc = new TableCell();
            tc.Height = 69;
            tc.HorizontalAlign = HorizontalAlign.Right;
            tc.VerticalAlign = VerticalAlign.Top;
            tc.Text = "简介：";
            tr[7].Cells.Add(tc);

            tc = new TableCell();
            tc.ColumnSpan = 2;
            tc.HorizontalAlign = HorizontalAlign.Left;
            tc.Text = video.VideoSummary;
            tr[7].Cells.Add(tc);

            tc = new TableCell();
            tc.Height = 47;
            tc.ColumnSpan = 3;
            tc.HorizontalAlign = HorizontalAlign.Center;
            tc.Text = "<h1> <a href='./videoLib.aspx?videoId=" + video.VideoId + "'> <img src='images/playBtn.png' alt='' width='150' height='40' />&nbsp;</a><a href='../movies/" + video.VideoName + ".rar ' ><img src='images/downloadBtn.png' width='150' height='40' /></a></h1>";
            tr[8].Cells.Add(tc);

            foreach (TableRow r in tr)
            {
                table.Rows.Add(r);
            }
        }

        public static int FillBSSearchTable(Table table, string keyType, string keyWord, int pageNo)
        {
            IDAL.IVideoDAL videoDAL = DALFactory.VideoDALFactory.CreatInstance("SQLServerVideoDAL");
            List<Video> videoList = videoDAL.SearchVideos(keyType, keyWord);
            int beginItemNo = (pageNo - 1) * 5;
            int endItemNo = ((pageNo - 1) * 5 + 5) < (videoList.Count) ? ((pageNo - 1) * 5 + 5) : (videoList.Count);
            for (int i = beginItemNo ; i < endItemNo; ++i)
            {
                Video video = videoList[i];

                TableRow tr = new TableRow();
                tr.BackColor = System.Drawing.Color.White;


                TableCell[] tc = new TableCell[5];

                for (int j = 0; j != 5; ++j)
                {
                    tc[j] = new TableCell();
                }

                tc[0].Height = 20;
                tc[0].VerticalAlign = VerticalAlign.Middle;
                tc[0].HorizontalAlign = HorizontalAlign.Center;
                tc[0].Text = video.VideoId.ToString();
                tr.Cells.Add(tc[0]);

                tc[1].VerticalAlign = VerticalAlign.Middle;
                tc[1].HorizontalAlign = HorizontalAlign.Center;
                tc[1].Text = video.VideoName;
                tr.Cells.Add(tc[1]);

                tc[2].VerticalAlign = VerticalAlign.Middle;
                tc[2].HorizontalAlign = HorizontalAlign.Center;
                tc[2].Text = video.VideoType.ToString();
                tr.Cells.Add(tc[2]);

                tc[3].VerticalAlign = VerticalAlign.Middle;
                tc[3].HorizontalAlign = HorizontalAlign.Center;
                tc[3].Text = video.VideoDirector;
                tr.Cells.Add(tc[3]);

                tc[4].VerticalAlign = VerticalAlign.Middle;
                tc[4].HorizontalAlign = HorizontalAlign.Center;
                tc[4].Text = "<a href='../../frontStage/movieDetailedInfo.aspx?videoId=" + video.VideoId + "' target=_blank>查看|</a><a href='./updateVideo.aspx?videoId=" + video.VideoId + "'>编辑|</a><a href='deleteVideo.aspx?videoId=" + video.VideoId + "&keyType=" + keyType + "&keyWord=" + keyWord + "&pageNo=" + pageNo + "'>删除</a>";
                tr.Cells.Add(tc[4]);

                table.Rows.Add(tr);

            }

            // 加上分页控制
            TableRow pageNoTr = new TableRow();
            pageNoTr.BackColor = System.Drawing.Color.White;
            TableCell pageNoTc_left = new TableCell();

            pageNoTc_left.ColumnSpan = 5;
            pageNoTc_left.VerticalAlign = VerticalAlign.Middle;
            pageNoTc_left.HorizontalAlign = HorizontalAlign.Center;
            int TotalPageNum = (videoList.Count/5 + 1);
            String pageNoStr = "第" + pageNo + " / " + TotalPageNum +" 页";
            for (int j = 0; j < TotalPageNum; ++j)
            {
                if (j + 1 != pageNo)
                {
                    pageNoStr += "<a href='./searchVideo.aspx?keyType=" + keyType + "&keyWord=" + keyWord + "&pageNo=" + (j+1) + "'>[" + (j+1) + "]</a>&nbsp;";
                }
                else
                {
                    pageNoStr += " " + (j + 1) + " ";
                }
            }
            pageNoTc_left.Text = pageNoStr;
            pageNoTr.Cells.Add(pageNoTc_left);
            table.Rows.Add(pageNoTr);
            return videoList.Count;
            

        }

    }
}
